package com.qysoft.dtkj.dtkj_gl.services.xtwh;

import java.math.BigDecimal;
import java.util.List;

import com.qysoft.rapid.dao.mybatis.RapidDao;
import com.qysoft.rapid.domain.Bean;
import com.qysoft.rapid.exceptions.BizException;

public class ZYGLService {


	/**
	 * 保存资源信息
	 * @param zyxx
	 * @throws Exception
	 */
	public static void saveZYXX(Bean zyxx) throws Exception{
		//初始化zyid
		String id = RapidDao.selectOneByXml("zyid_select", zyxx.getStr("parentid"));
		zyxx.set("id", id);
		//初始化资源排序
		if (zyxx.get("px")==null||"".equals(zyxx.getStr("px").trim())) {
			zyxx.set("px",Integer.parseInt(id.substring(id.length()-2)));
		}
		RapidDao.save("sys_priv", "id", zyxx);
	}	
	
	/**
	 * 删除资源
	 * @param zyid 资源id
	 * @throws Exception
	 */
	public static void delete(String zyid) throws Exception{
		BigDecimal count = (BigDecimal) RapidDao.queryOne("select count(1) from sys_priv t where t.parentid = ?", zyid);
		if (count.longValue()>0) {
			throw new BizException("删除失败，存在下级资源！");
		}
		RapidDao.deleteById("sys_role_priv", "privid", zyid);
		RapidDao.deleteById("sys_priv", "id",zyid);
	}
	
	/**
	 * 查询资源信息
	 * @param jgid 机构id
	 * @return
	 * @throws Exception
	 */
	public static Bean queryZYXX(String zyid) throws Exception{
		Bean zyxx = RapidDao.selectOneByXml("zyxx_select", zyid);
		return zyxx;
	}
	
	/**
	 * 修改资源信息
	 * @param jgxx
	 * @throws Exception
	 */
	public static void updateZYXX(Bean zyxx) throws Exception{
		//不可修改上级资源
		zyxx.remove("parentid");
		RapidDao.update("sys_priv", "id", zyxx);
	}	
		
	/**
	 * 查询资源信息
	 * @param jsid
	 * @return
	 * @throws Exception
	 */
	public static List<Bean> queryZyxxForTree() throws Exception{ 
		List<Bean> zyxxList = RapidDao.selectListByXml("zyxx_selectForTree");
		return zyxxList;
	}
	
	/**
	 * 查询角色已授权的资源id
	 * @param jsid
	 * @return
	 * @throws Exception
	 */
	public static String queryJsZy(String jsid) throws Exception{
		String zyids = (String) RapidDao.queryOne("select to_char(wm_concat(t.privid)) from sys_role_priv t where t.roleid = ?", jsid);
		return zyids;
	}
	
	public static List<String> queryAllZylj() throws Exception{
		List<String> zyljs = RapidDao.queryList("select url from sys_priv");
		return zyljs;
	}
	
	/**
	 * 根据人员代码查询权限内的资源
	 * @param rydm
	 * @return
	 * @throws Exception 
	 */
	public static List<String> queryZyljByRydm(String rydm) throws Exception{
		StringBuilder sql = new StringBuilder();
		sql.append("select distinct d.url ");
		sql.append("from sys_user a, sys_user_role b, sys_role_priv c, sys_priv d ");
		sql.append("where a.id = b.userid ");
		sql.append("and b.roleid = c.roleid ");
		sql.append("and c.privid = d.id ");
		sql.append("and a.username = ? ");
		sql.append("and d.url is not null ");
		List<String> zyljs = RapidDao.queryList(sql.toString(), rydm); 
		return zyljs;
	}
	
	/**
	 * 根据人员代码查询权限内的资源
	 * @param rydm
	 * @return
	 * @throws Exception 
	 */
	public static List<Bean> queryZyljForMain(String rydm) throws Exception{
		//查询一级资源
		StringBuilder sql = new StringBuilder();
		sql.append("select * ");
		sql.append(" from (select distinct c.id as \"zyid\", c.name as \"zymc\", c.url as \"zylj\" ,c.css as \"zytb\",c.px as \"px\" ");
		sql.append(" from sys_user_role a, sys_role_priv b, sys_priv c ");
		sql.append(" where a.roleid = b.roleid ");
		sql.append(" and b.privid = c.id ");
		sql.append(" and c.parentid = ? ");
		sql.append(" and a.userid = ? ");
		sql.append(" and c.node_type = '01' )");
		sql.append(" order by \"px\" asc");
		List<Bean> yjzyList = RapidDao.queryList(sql.toString(), "00",rydm);	//一级资源
		for (Bean yjzy : yjzyList) {
			List<Bean> ejzyList = RapidDao.queryList(sql.toString(),yjzy.getStr("zyid"),rydm);	//二级资源
			yjzy.set("ejzySize", ejzyList.size());
			yjzy.set("ejzyList", ejzyList);
		}
		return yjzyList;
	}
	
	/**
	 * 根据人员代码查询权限内的资源
	 * @param rydm
	 * @return
	 * @throws Exception 
	 */
	public static List<Bean> queryZyljForCscl(String rydm) throws Exception{
		//查询一级资源
		StringBuilder yjsql = new StringBuilder();
		yjsql.append("select distinct c.zyid,c.zymc,c.zylj,c.zytb from ");
		yjsql.append("yygl_ry_jsxx a,xtwh_jsqx b,xtwh_zyxx c ");
		yjsql.append("where a.jsid = b.jsid ");
		yjsql.append("and b.zyid = c.zyid ");
		yjsql.append("and c.zylx = ? ");
		yjsql.append("and c.sjzyid = (select zyid from xtwh_zyxx where sfxs = '0') ");
		yjsql.append("and a.rydm = ? ");
		yjsql.append("order by c.zypx asc ");
		List<Bean> yjzyList = RapidDao.queryList(yjsql.toString(), "0",rydm);	//一级资源
		StringBuilder ejsql = new StringBuilder();
		ejsql.append("select distinct c.zyid,c.zymc,c.zylj,c.zytb from ");
		ejsql.append("yygl_ry_jsxx a,xtwh_jsqx b,xtwh_zyxx c ");
		ejsql.append("where a.jsid = b.jsid ");
		ejsql.append("and b.zyid = c.zyid ");
		ejsql.append("and c.zylx = ? ");
		ejsql.append("and c.sjzyid = ? ");
		ejsql.append("and a.rydm = ? ");
		ejsql.append("order by c.zypx asc ");
		for (Bean yjzy : yjzyList) {
			List<Bean> ejzyList = RapidDao.queryList(ejsql.toString(), "0",yjzy.getStr("zyid"),rydm);	//二级资源
			yjzy.set("ejzySize", ejzyList.size());
			yjzy.set("ejzyList", ejzyList);
		}
		return yjzyList;
	}		
}
